package com.example.zjhuojback.mapper;

import com.example.zjhuojback.entity.Contest;
import com.example.zjhuojback.entity.ContestProblem;
import com.example.zjhuojback.entity.ContestSubmission;
import com.example.zjhuojback.entity.Forum;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ContestMapper {

    // 查询
    @Select("SELECT * FROM `contest` ORDER BY id DESC")
    List<Contest> selectAll();

    // 根据比赛id搜索比赛内容
    @Select("select * from `contest` where id = #{id}")
    Contest selectById(@Param("id") Integer id);


    // 创建比赛
    @Insert("insert into `contest` (name, startTime, endTime) " +
            "values (#{name}, #{startTime}, #{endTime})")
    void insert(Contest contest);

    // 更新比赛
    @Update("update `contest` set name = #{name}, startTime = #{startTime}, endTime = #{endTime} where id = #{id}")
    void update(Contest contest);

    // 更新比赛状态
    @Update("update `contest` set status = #{status} where id = #{id}")
    void updateStatus(@Param("id") Integer id, @Param("status") Integer status);
    // 查询比赛题目
    @Select("select * from `contestproblem` where contestId = #{contestId}")
    List<ContestProblem> getQuestions(@Param("contestId") Integer contestId);

    // 根据帖子id删除帖子内容
    @Delete("delete from `contestproblem` where contestId = #{contestId}")
    void removeProblem(@Param("contestId")Integer contestId);

    // 添加评论
    @Insert("insert into `contestproblem` (contestId, problemId, problemTitle) " +
            "values (#{contestId}, #{problemId}, #{problemTitle})")
    void insertProblem(ContestProblem contestProblem);

    // 插入用户比赛时间提交记录
    @Insert("insert into `contestsubmission` (contestId, problemId, userId, submitTime, isCorrect) " +
            "values (#{contestId}, #{problemId}, #{userId}, #{submitTime}, #{isCorrect})")
    void insertContestSubmission(ContestSubmission contestSubmission);

    // 提交数+1
    @Update("UPDATE `contestproblem` SET submitCount = submitCount + 1 WHERE contestId = #{contestId} AND problemId = #{problemId}")
    void updateSubmit(@Param("contestId") Integer contestId, @Param("problemId") Integer problemId);
    // AC数+1
    @Update("UPDATE `contestproblem` SET submitCount = submitCount + 1, correctCount = correctCount + 1 WHERE contestId = #{contestId} AND problemId = #{problemId}")
    void updateAccept(@Param("contestId") Integer contestId, @Param("problemId") Integer problemId);

    // 获取所有提交信息
    @Select("SELECT * FROM `contestproblem` ORDER BY id ASC where contestId = #{contestId}")
    List<ContestSubmission> selectAllSubmission(@Param("contestId")Integer contestId);

    // 判断用户是否AC这道题
    @Select("SELECT * FROM `contestsubmission` WHERE contestId = #{contestId} AND problemId = #{problemId} AND userId = #{userId} AND isCorrect = 1 ORDER BY submitTime DESC")
    List<ContestSubmission> findByContestProblemAndUser(@Param("contestId") Integer contestId,
                                                  @Param("problemId") Integer problemId,
                                                  @Param("userId") Integer userId);


    // 根据比赛id获得参与用户的id
    @Select("SELECT DISTINCT userId FROM `contestsubmission` WHERE contestId = #{contestId} ")
    List<Integer> getUserIdsByContestId(@Param("contestId") Integer contestId);

}
